package com.wang.rabbitmq.springboot.deadqueue;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @author: wang
 * @date: 2021/8/4 19:36
 * @description:
 */
@Configuration
public class DeadConfig {


    // =============== 设置死信队列
    @Bean
    public DirectExchange getDeadDirectExchange(){
        return new DirectExchange("deadDirectExchange");
    }

    @Bean
    public Queue getDirectDeadQueue(){
        return new org.springframework.amqp.core.Queue("deadDirectQueue", true, false, false);
    }

    @Bean
    public Binding deadBind(Queue getDirectDeadQueue, DirectExchange getDeadDirectExchange){
        return BindingBuilder.bind(getDirectDeadQueue).to(getDeadDirectExchange).with("deadQueue-key");
    }

    // =========== 设置direct队列

    @Bean
    public DirectExchange getDirectExchange(){
        return new DirectExchange("directExchange");
    }

    @Bean
    public Queue getDirectQueue(){
        HashMap<String, Object> arguments = new HashMap<String, Object>(2);
        // 设置死信交换机
        arguments.put("x-dead-letter-exchange","deadDirectExchange");
        //设置死信队列的routingkey
        arguments.put("x-dead-letter-routing-key","deadQueue-key");
        //设置为ttl及过期时间
        arguments.put("x-message-ttl",5000);
        //设置队列最多能接收的消息数
        arguments.put("x-max-length",10);
        return new Queue("directQueue",true,false,true,arguments);
    }

    @Bean
    public Binding directBind(Queue getDirectQueue, DirectExchange getDirectExchange){
        return BindingBuilder.bind(getDirectQueue).to(getDirectExchange).with("directQueue-key");
    }


}
